package com.hk.service.impl;

import com.github.pagehelper.PageHelper;
import com.hk.mapper.IProductMapper;
import com.hk.model.Product;
import com.hk.model.ProductMenu;
import com.hk.service.IProductMenuService;
import com.hk.service.IProductService;
import com.hk.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: zhe
 * @Date: 2021/6/19 12:40
 * @Statement: 产品Service层接口实现类
 */
@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    IProductMapper iProductMapper;

    @Autowired
    IProductMenuService iProductMenuService;

    @Override
    public List<Product> findAll(Integer page, Integer limit, Product t) {
        PageHelper.startPage(page,limit);
        List<Product> productList = iProductMapper.findAll(t);
//        查询所有产品类型,匹配对应产品类型及父类型
        List<ProductMenu> productMenuList = iProductMenuService.findAll(-1, -1, new ProductMenu());
//        遍历产品
        for (Product product : productList) {
//            遍历类型
            for (ProductMenu productMenu : productMenuList) {
//                匹配子类型
                if (product.gettMenuId() == productMenu.getId()) {
                    product.settMenuName(productMenu.getName());
                    getMatching1(product,productMenuList);
                    break;
                }
                if (productMenu.getProductMenus().size() > 0) {
                    getMatching(productMenu, product);
//                    当类型匹配上后结束本次循环
                    if (product.gettMenuName() != null) {
                        getMatching1(product,productMenuList);
                        break;
                    }
                }
            }
            if (product.gettMenuName() == null) {
                product.settMenuName("暂无类型");
            }
            if (product.getMenuName() == null) {
                product.setMenuName("暂无父类型");
            }
        }
        return productList;
    }

    /**
     * 递归判断产品属于那个类型
     *
     * @param productMenu
     * @param product     存储匹配的节点名称
     */
    public void getMatching(ProductMenu productMenu, Product product) {
//        判断是否具有子节点
        if (productMenu.getProductMenus().size() > 0) {
            for (ProductMenu type : productMenu.getProductMenus()) {
//                匹配类型
                if (product.gettMenuId() == type.getId()) {
                    product.settMenuName(type.getName());
//                    递归查询父节点类型
                    getMatching1(product,productMenu.getProductMenus());
                    break;
                }
//                未匹配到继续递归搜索
                getMatching(type, product);
            }
        }
    }

    /**
     * 递归判断产品属于那个父类型
     *
     * @param productMenuList 所有节点
     * @param product         存储匹配的节点名称
     */
    public void getMatching1(Product product, List<ProductMenu> productMenuList) {
//        判断是否具有子节点
        if (productMenuList.size() > 0) {
            for (ProductMenu type : productMenuList) {
//                匹配父类型
                if (product.getMenuId() == type.getId()) {
                    product.setMenuName(type.getName());
                    break;
                }
                if (type.getProductMenus().size() > 0) {
                    //                未匹配到，继续递归搜索
                    getMatching1(product,type.getProductMenus());
                }
            }
        }
    }

    @Override
    public int count(Integer page, Integer limit, Product t) {
//        PageHelper.startPage(page,limit);
        int count = iProductMapper.count(t);
        return count;
    }

    @Override
    public Product findById(Integer id) {
        Product product = iProductMapper.findById(id);
        return product;
    }

    @Override
    public List<Product> findAllImg() {
        List<Product> productList = iProductMapper.findAll(new Product());
        return productList;
    }

    @Override
    public List<Product> findByIds(Integer id) {
        List<Product> productList = iProductMapper.findByIds(id);
        return productList;
    }

    @Override
    public int save(Product t) {
//        获取子节点所属的根节点Id
        int parentId = iProductMenuService.findById(t.gettMenuId()).getParentId();
        t.setMenuId(parentId);
//        封装创建时间和修改时间
        String dateTimeString = DateUtils.dateTimeString(new Date());
        t.setGmtCreate(dateTimeString);
        t.setGmtModified(dateTimeString);
        int save = iProductMapper.save(t);
        if (save == 1) {
            System.out.println("添加成功");
        }
        return save;
    }

    @Override
    public int update(Product t) {
        int parentId = iProductMenuService.findById(t.gettMenuId()).getParentId();
        t.setMenuId(parentId);
        //        修改日期时间
        String dateTimeString = DateUtils.dateTimeString(new Date());
        t.setGmtModified(dateTimeString);
        int update = iProductMapper.update(t);
        if (update == 1) {
            System.out.println("产品信息修改成功");
        }
        return update;
    }

    @Override
    public int delete(Integer id) {
        int delete = iProductMapper.delete(id);
        if (delete == 1) {
            System.out.println("产品信息删除成功");
        }
        return delete;
    }

}
